package com.ABdolphin.forum.services.impl;

import com.ABdolphin.forum.common.AppResult;
import com.ABdolphin.forum.common.ResultCode;
import com.ABdolphin.forum.dao.MessageMapper;
import com.ABdolphin.forum.exception.ApplicationException;
import com.ABdolphin.forum.model.Message;
import com.ABdolphin.forum.model.User;
import com.ABdolphin.forum.services.IMessageService;
import com.ABdolphin.forum.services.IUserService;
import com.ABdolphin.forum.utils.StringUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class MessageServiceImpl implements IMessageService {
    @Resource
    private MessageMapper messageMapper;
    @Resource
    private IUserService userService;
    @Override
    public void create(Message message) {
        if (message==null||message.getPostUserId()==null
                ||message.getReceiveUserId()==null
                || StringUtil.isEmpty(message.getContent())){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //校验接受者是否存在
        User user = userService.selectById(message.getReceiveUserId());
        if (user==null||user.getDeleteState()==1){
            throw new ApplicationException(AppResult.failed("该用户不存在"));
        }
        message.setState((byte)0);//默认未读
        message.setDeleteState((byte)0);
        Date date=new Date();
        message.setCreateTime(date);
        message.setUpdateTime(date);
        int row = messageMapper.insertSelective(message);
        if (row!=1){
            log.warn(ResultCode.FAILED.toString()+", 受影响行数不等于1,");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public Integer selectUnreadCount(Long receiveUserId) {
        if (receiveUserId==null||receiveUserId<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Integer count = messageMapper.selectUnreadCount(receiveUserId);
        if (count==null){
            //正常的查询不可能出现null
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        return count;
    }

    @Override
    public List<Message> selectByReceiveId(Long receiveUserId) {
        if (receiveUserId==null||receiveUserId<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        List<Message> messages = messageMapper.selectByReceiveId(receiveUserId);
        return messages;
    }

    @Override
    public void updateStateById(Long id, Byte state) {
        //0未读,1,已读.2已回复
        if (id==null || id<=0 || state<0 || state>2){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Message updateMessage=new Message();
        updateMessage.setId(id);
        updateMessage.setState(state);
        Date date=new Date();
        updateMessage.setUpdateTime(date);
        int row = messageMapper.updateByPrimaryKeySelective(updateMessage);
        if (row!=1){
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public Message selectById(Long id) {
        if (id==null||id<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Message message = messageMapper.selectByPrimaryKey(id);
        return message;
    }

    @Override
    public void reply(Long repliedId, Message message) {
        if (repliedId==null||repliedId<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Message existMessage = messageMapper.selectByPrimaryKey(repliedId);
        if (existMessage==null){
            log.warn(ResultCode.FAILED_MESSAGE_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS));
        }
        //更新状态为已回复
        updateStateById(repliedId,(byte)2);
        create(message);
    }
}































